package bsf.baseservice.businessmq.db;

import bsf.baseservice.businessmq.model.tb_mqpath_model;
import bsf.baseservice.businessmq.dal.tb_config_dal;
import bsf.baseservice.businessmq.dal.tb_datanode_dal;
import bsf.baseservice.businessmq.dal.tb_mqpath_dal;
import bsf.baseservice.businessmq.model.tb_config_model;
import bsf.baseservice.businessmq.model.tb_datanode_model;
import bsf.baseservice.businessmq.systemruntime.BusinessMQException;
import bsf.baseservice.businessmq.systemruntime.SystemParamConfig;
import bsf.db.DbConn;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;

public class BaseBLL
{
	//public virtual string GetRedisServerIP(DbConn PubConn)
	//{
	//    string serverip = GetConfigValue(PubConn, EnumSystemConfigKey.RedisServer.ToString());
	//    return serverip;
	//}

	public String getDataNodeConnectString(String template, tb_datanode_model model)
	{
		return template.replace("{server}", model.serverip).replace("{password}", model.password)
				.replace("{username}", model.username)
				.replace("{database}", SystemParamConfig.DataNode_DataBaseName_Prefix + StringUtils.leftPad((model.datanodepartition+""),2,'0'))
				.replace("{server}", model.serverip);

	}

	//public virtual string GetConfigValue(DbConn PubConn, string key)
	//{
	//    tb_config_dal dal = new tb_config_dal();
	//    var value = dal.Get2(PubConn, key);
	//    return value;
	//}

	public java.util.ArrayList<tb_config_model> getConfig(DbConn PubConn)
	{
		tb_config_dal dal = new tb_config_dal();
		java.util.ArrayList<tb_config_model> rs = dal.list(PubConn);
		return rs;
	}

	public tb_mqpath_model getMqPathModel(DbConn PubConn, String mqpath)
	{
		tb_mqpath_dal mqpathdal = new tb_mqpath_dal();
		tb_mqpath_model mqpathmodel = mqpathdal.get(PubConn, mqpath);
		if (mqpathmodel == null)
		{
			throw new BusinessMQException("当前队列不存在:" + mqpath);
		}
		return mqpathmodel;
	}

	public  java.util.HashMap<Integer, tb_datanode_model> getDataNodeModelsDic(DbConn PubConn, ArrayList<Integer> datanodepartition)
	{

		tb_datanode_dal datanodedal = new tb_datanode_dal();
		java.util.HashMap<Integer, tb_datanode_model> rs = new java.util.HashMap<Integer, tb_datanode_model>();
		for (tb_datanode_model o : datanodedal.list(PubConn, datanodepartition))
		{
			rs.put(o.datanodepartition, o);
		}
		return rs;
	}
}